import base64
import hashlib
import hmac
import json
import time
import urllib.parse

import requests

url = 'https://oapi.dingtalk.com/robot/send?access_token=54e0c21de9b50a1f7d364186bbbbf597d7660953c99e0afd511b8e4787aab2f1'
timestamp = str(round(time.time() * 1000))
secret = 'SECba4d0abb9d13f40a339748741b76a75352f97d084fa99dc8796be6af0c63a5f0'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = url + '&timestamp=' + timestamp + '&sign=' + sign
data = {
    'msgtype': 'markdown',
    'markdown': {
        "title": '天线宝宝',
        'text': '## 宝宝们打卡喽：\n## 1、上班卡两个呦\n## 2、禅道开启了嘛\n## 3、要有安全意识哦,打卡！'
    },
    'at': {
        "atMobiles": [],
        "atUserIds": [],
        "isAtAll": True
    }
}
print(data)
headers = {'Content-Type': 'application/json', }
r = requests.post(url, data=json.dumps(data), headers=headers)
res = json.loads(r.text)
print(res)
